import { BoolType, IModuleWithState } from '../types'
import { setDomBoolVal } from '../utils'
import { au } from './au'

interface IPswState {
    g_en: BoolType
    gf: BoolType
}

interface IPsw extends IModuleWithState<IPswState> {}

/**
 * psw模块
 */
export const psw: IPsw = {
    name: 'psw',
    state: {
        g_en: 0,
        gf: 0,
    },
    async setState(prop, value) {
        this.state[prop] = value
        setDomBoolVal(prop, this.state[prop] as BoolType)
        return
    },
    async neg() {
        // 在时钟下降沿触发时
        if (this.state.g_en === 1) {
            const gf = au.state.au_gf
            this.setState('gf', gf)
        }
    },
    init() {
        // 设置g_en的初始值
        setDomBoolVal('g_en', this.state.g_en)
        // 设置gf的初始值
        setDomBoolVal('gf', this.state.gf)
    },
}
